'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const m = require('../nodes/Alias.cjs'),
  s = require('../nodes/identity.cjs'),
  j = require('../nodes/Scalar.cjs'),
  A = 'tag:yaml.org,2002:';
function P(n, i, t) {
  if (i) {
    const r = t.filter(c => c.tag === i),
      f = r.find(c => !c.format) ?? r[0];
    if (!f) throw new Error(`Tag ${i} not found`);
    return f;
  }
  return t.find(r => {
    var f;
    return ((f = r.identify) == null ? void 0 : f.call(r, n)) && !r.format;
  });
}
function T(n, i, t) {
  var p, S, O;
  if ((s.isDocument(n) && (n = n.contents), s.isNode(n))) return n;
  if (s.isPair(n)) {
    const d =
      (S = (p = t.schema[s.MAP]).createNode) == null
        ? void 0
        : S.call(p, t.schema, null, t);
    return d.items.push(n), d;
  }
  (n instanceof String ||
    n instanceof Number ||
    n instanceof Boolean ||
    (typeof BigInt < 'u' && n instanceof BigInt)) &&
    (n = n.valueOf());
  const {
    aliasDuplicateObjects: r,
    onAnchor: f,
    onTagObj: c,
    schema: h,
    sourceObjects: b,
  } = t;
  let e;
  if (r && n && typeof n == 'object') {
    if (((e = b.get(n)), e))
      return e.anchor || (e.anchor = f(n)), new m.Alias(e.anchor);
    (e = { anchor: null, node: null }), b.set(n, e);
  }
  i != null && i.startsWith('!!') && (i = A + i.slice(2));
  let o = P(n, i, h.tags);
  if (!o) {
    if (
      (n && typeof n.toJSON == 'function' && (n = n.toJSON()),
      !n || typeof n != 'object')
    ) {
      const d = new j.Scalar(n);
      return e && (e.node = d), d;
    }
    o =
      n instanceof Map
        ? h[s.MAP]
        : Symbol.iterator in Object(n)
          ? h[s.SEQ]
          : h[s.MAP];
  }
  c && (c(o), delete t.onTagObj);
  const y =
    o != null && o.createNode
      ? o.createNode(t.schema, n, t)
      : typeof ((O = o == null ? void 0 : o.nodeClass) == null
            ? void 0
            : O.from) == 'function'
        ? o.nodeClass.from(t.schema, n, t)
        : new j.Scalar(n);
  return i ? (y.tag = i) : o.default || (y.tag = o.tag), e && (e.node = y), y;
}
exports.createNode = T;
